<?php

/**
 * @package urCronPlugin
 * @subpackage urCronServiceGoogleReaderSharedPlugin
 * @author	Marc Weistrof <marc.weistroff@gmail.com>
 * @version $Id: urCronServiceGoogleReaderShared.class.php 5 2009-06-09 09:15:55Z flambeur $
 */
class urCronServiceGoogleReaderShared extends urCronServiceBase
{
  public function __construct()
  {
    Zend_Feed::registerNamespace("gr", "http://www.google.com/schemas/reader/atom/");
    Zend_Feed::registerNamespace("media", "http://search.yahoo.com/mrss/");
  }

  /**
   * Return the service name and the associated class name.
   * @static
   * @return Array
   */
  static function getServiceName()
  {
    return array('Google Reader Shared Items' => 'urCronServiceGoogleReaderShared');
  }
  
  public function doCronJobAtom($continuation = null)
  {

    $feedUri = $this->feedUri;
    if( $continuation != null )
      $feedUri .= '?c=' . $feedUri;
    
    $this->feed = Zend_Feed::import($feedUri);
    
    foreach($this->feed->entry as $item)
    {
      // We have to check if the item is already in the database
      $exists = Doctrine::getTable('urCronItem')->fetchOneByOriginalId($item->id());
      if($exists)
        continue;

      $object = new urCronItem();

      $publishedDate = date("Y-m-d H:i:s", strtotime($item->published()));
      
      $data = array('title'                   => $item->title(),
                    'ur_cron_feed_id'         => $this->feedId,
                    'original_id'             => $item->id(),
                    'original_title'          => $item->title(),
                    'original_content'        => $item->content(),
                    'original_link'           => $item->link('alternate'),
                    'original_author'         => $item->author->name(),
                    'original_summary'        => $item->summary(),
                    'original_published_date' => $publishedDate,
                    'is_published'            => sfConfig::get('app_urcron_autopublish', true)
                   );
      
      $object->addTags($this->parseTags($item));

      $sourceName = $item->source->title();
      $sourceUrl = $item->source->link->getDOM()->getAttribute("href");

      $this->cleanContent($object);
      $object->addData('source_name', $sourceName);
      $object->addData('source_url', $sourceUrl);
      $object->save();
    }

    if(!$exists)
    {
      $continuation = $this->feed->continuation();
      $this->doCronJobAtom($continuation);
    }
  }

  protected function cleanContent($item)
  {
    ProjectConfiguration::registerHTMLPurifier();

    $content = $item->getOriginalContent() != "" ? $item->getOriginalContent() : $item->getOriginalSummary();

    $config  = HTMLPurifier_Config::createDefault();
    $config->set('HTML', 'DefinitionID', 'google shared');
    $config->set('HTML', 'DefinitionRev', 1);
    $config->set('Filter', 'YouTube', true);
    $config->set('HTML', 'ForbiddenAttributes', "img@style");
    $config->set('HTML', 'ForbiddenElements', 'center, div');
    $config->set('URI', 'HostBlacklist', 'feedburner.com');

    $purifier = new HTMLPurifier( $config );
    $context = new HTMLPurifier_Context();
    $lexer = new HTMLPurifier_Lexer_DirectLex();

    $content = $purifier->purify($content);
    $tokens = $lexer->tokenizeHTML($content, $config, $context);

    $cc = "";
    $pCount = 0;
    foreach($tokens as $token)
    {
      if($token instanceof HTMLPurifier_Token_Start)
      {
        $cc .= '<' . $token->name;
        foreach($token->attr as $k => $v)
          $cc .= ' '.$k.'='.'"'.$v.'"';
        $cc .= ">";
      }
      else if($token instanceof HTMLPurifier_Token_End)
      {
        $cc .= '</'.$token->name.'>';
        if($token->name == "p")
          $pCount++;
      }
      else if($token instanceof HTMLPurifier_Token_Text)
      {
        $cc .= $token->data;
      }
      else if($token instanceof HTMLPurifier_Token_Empty)
      {
        $cc .= "<".$token->name;
        foreach( $token->attr as $k => $v )
        {
          $cc .= ' '.$k.'='.'"'.$v.'"';
        }
        $cc .= ' />';
      }
      
      if($pCount == 3)
        break;
    }

    $item->setContent($cc);
  }
}

?>
